<template>
  <header class="header">
    <h1>todos</h1>
    <input id="toggle-all" class="toggle-all" v-model="isAll" type="checkbox" />
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      v-model="taskName"
      @keydown.enter="enterDow"
      placeholder="输入任务名称-回车确认"
      autofocus
    />
  </header>
</template>

<script>
export default {
  data() {
    return {
      taskName: ''
    }
  },
  props: ['list'],
  methods: {
    enterDow() {
      if (this.taskName.length === 0) return alert('请输入内容')
      this.$emit('addTask', this.taskName)
      this.taskName = ''
    }
  },
  computed: {
    isAll: {
      set(val) {
        // 单项数据流不能直接更改 props 里的值，如果是一个引用数据类型，则可以更改里面的属性值
        this.list.forEach(item => (item.isDone = val))
      },
      get() {
        if (this.list.length === 0) return false
        return this.list.every(item => item.isDone)
      }
    }
  }
}
</script>
